//添加编制车辆
var setHtml=`<div class="layui-form-item set">
<div class="layui-form-item">
    <div class="layui-inline">
        <label class="layui-form-label"></label>
        <div class="layui-input-inline" style="width: 100px;">
            <input type="text"  placeholder="设备名称" autocomplete="off" class="layui-input setname" readonly>
        </div>
        <div class="layui-form-mid">-</div>
        <div class="layui-input-inline" style="width: 100px;">
            <input type="text"  placeholder="用途" autocomplete="off" class="layui-input setuse" readonly>
        </div>
    </div>
</div>
</div> `;
var id ='{$Think.get.id}'
// 循环遍历
if(id){
    Load();
	ajax(api.compile.list,{
		where_json:JSON.stringify({
			id:['in',"{$Think.get.id}"]
		})
	},'post').done(function(res) {
        console.log(res)
        Load(1);
		for(var key in res.data[0]){
            if(key == 'company'){
                $('input[name=company_id]').val(res.data[0]['company'].company_id)
                $('input[name=company_name]').val(res.data[0]['company'].company_name)
            }else if(key == 'strength_type'){              
                    if(res.data[0][key]==1){
                        $('.strength_type_name').val('执法执勤用车')
                       
                    }else if(res.data[0][key]==2){
                        $('.strength_type_name').val('特种专业技术用车')
                       
                    }else if(res.data[0][key]==3){
                        $('.strength_type_name').val('机要通信用车')
                      
                    }else if(res.data[0][key]==4){
                        $('.strength_type_name').val('应急保障用车')
                     
                    }
            }else if(key == 'approval_document'){
                if(res.data[0][key]){
                    var imgData = JSON.parse(res.data[0][key])
                    if(imgData.approval_img){
                        $('#img1').attr('src',domain+imgData.approval_img)
                    }else{
                        $('#img1').attr('src','/assets/img/noimg.gif')
                    }
                    if(imgData.budget_img){
                        $('#img2').attr('src',domain+imgData.budget_img)
                    }else{
                        $('#img2').attr('src','/assets/img/noimg.gif')
                    }               
                }else{
                    $('#img1').attr('src','/assets/img/noimg.gif')
                    $('#img2').attr('src','/assets/img/noimg.gif')
                }

            }else if (key == 'car'){
                console.log(res.data[0]['car'].number)
                $('input[name=car_id]').val(res.data[0]['car'].car_id)
                $('input[name=number]').val(res.data[0]['car'].number)
            }else if(key =='standard'){
                let standardData= JSON.parse(res.data[0][key]);

                for(keys in standardData){
                    if(keys =='set'){
                for(var i =0;i<standardData[keys].length;i++){
                    if(i>0){
                       $('.set').after(setHtml) 
                    }
                    $('.setname').eq(i).val(standardData[keys][i].setname)
                    $('.setuse').eq(i).val(standardData[keys][i].setuse)
                }
                    }else if(keys == 'new_energy'){
                        if(standardData[keys]==1){
                            console.log(1)
                            $(`.${keys}`).attr('checked',true)
                        }else{
                            $(`.${keys}`).attr('checked',false)
                        }

                    }else if(keys =='car_type'){
                            if(standardData[keys]==1){
                                $('.car_type_name').val('大型客车')
                               
                            }else if(standardData[keys]==2){
                                $('.car_type_name').val('中型客车')
                               
                            }else if(standardData[keys]==3){
                                $('.car_type_name').val('小型客车')
                              
                            }
                        
                    }else{
                        $(`.${keys}`).val(standardData[keys])
                    }
                }

                for(keys in standardData){
                    if(keys == 'set'){                       
                    }else{
                        if(standardData.keys){
                            $(`.${keys}`).val(standardData[keys])
                        }else{
                            $(`.${keys}`).attr('placeholder','未填写')  
                        }
                        
                    }
                   
                }
                
            }else{
                if(res.data[0][key]){
                 $('input[name='+key+']').val(res.data[0][key])
                }else{
                    $('input[name='+key+']').attr('placeholder','未填写')                  
                }
            }
         
            
        }
        form.render()
	}).fail(function(err) {
		Err('请求失败')
    });
}else{
    Err('暂无数据')
}
var state ='{$Think.get.state}';
console.log(state)
if (Number(state) ==1){
    $('.pass').on('click',()=>{
        var reason =''
        form.on('submit(save)', function(data){
            console.log(data.field) //当前容器的全部表单字段，名值对形式：{name: value}
             reason = data.field.reason;
             return false; 
        })
       var Tip= layer.confirm('是否确定审核通过', {
            btn: ['确定','取消'] //按钮
          }, function(){
            ajax(api.compile.audit,{id:id,state:2,reason:reason},'post').done(function(res) {
                if(res.code ==1){
                    layer.msg('审核通过', {icon: 1});
                    window.location.href= "{:url('html/car/car_compile')}"
                }else{
                    layer.msg('审核失败,稍后再试', {icon: 2});
                }
            
            }).fail((err)=>{
                layer.msg('请求失败,稍后再试', {icon: 0});
            })
          }, function(){
           layer.close(Tip)
          });
    })
    
    $('.reject').on('click',()=>{
        var reason =''
        form.on('submit(save)', function(data){
            console.log(data.field) //当前容器的全部表单字段，名值对形式：{name: value}
             reason = data.field.reason;
             return false; 
        })
       var Tip= layer.confirm('是否确定驳回审核', {
            btn: ['确定','取消'] //按钮
          }, function(){
            ajax(api.compile.audit,{id:id,state:3,reason:reason},'post').done(function(res) {
                if(res.code ==1){
                    layer.msg('审核已驳回', {icon: 1});
                    window.location.href= "{:url('html/car/car_compile')}"
                }else{
                    layer.msg('审核失败,稍后再试', {icon: 2});
                }
            
            }).fail((err)=>{
                layer.msg('请求失败,稍后再试', {icon: 0});
            })
          }, function(){
           layer.close(Tip)
          });
    })
}else if(Number(state) ==4){
    $('.pass').on('click',()=>{
        var reason =''
        form.on('submit(save)', function(data){
            console.log(data.field) //当前容器的全部表单字段，名值对形式：{name: value}
             reason = data.field.reason;
             return false; 
        })
       var Tip= layer.confirm('是否同意更新配置', {
            btn: ['确定','取消'] //按钮
          }, function(){
            ajax(api.compile.audit,{id:id,state:6,reason:reason},'post').done(function(res) {
                if(res.code ==1){
                    layer.msg('审核通过', {icon: 1});
                    window.location.href= "{:url('html/car/car_compile')}"
                }else{
                    layer.msg('审核失败,稍后再试', {icon: 2});
                }
            
            }).fail((err)=>{
                layer.msg('请求失败,稍后再试', {icon: 0});
            })
          }, function(){
           layer.close(Tip)
          });
    })
    
    $('.reject').on('click',()=>{
        var reason =''
        form.on('submit(save)', function(data){
            console.log(data.field) //当前容器的全部表单字段，名值对形式：{name: value}
             reason = data.field.reason;
             return false; 
        })
       var Tip= layer.confirm('是否确定拒绝更新配置', {
            btn: ['确定','取消'] //按钮
          }, function(){
            ajax(api.compile.audit,{id:id,state:7,reason:reason},'post').done(function(res) {
                if(res.code ==1){
                    layer.msg('审核已驳回', {icon: 1});
                    window.location.href= "{:url('html/car/car_compile')}"
                }else{
                    layer.msg('审核失败,稍后再试', {icon: 2});
                }
            
            }).fail((err)=>{
                layer.msg('请求失败,稍后再试', {icon: 0});
            })
          }, function(){
           layer.close(Tip)
          });
    })
}

